package com.core.ui.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.core.managers.LoginManager;
import com.core.managers.SessionManager;
import com.core.protocols.IActionBarDatasource;
import com.core.protocols.ICustomDatasourceDelegate;
import com.core.protocols.IServiceLayerDelegate;
import com.core.servicelayer.ActionBarServiceLayer;
import com.core.ui.actionscript.ActionScriptParser;
import com.core.ui.controller.base.ServletDependenceController;
import com.core.ui.customview.ActionBarItemView;
import com.model.ActionBarItem;
import com.model.ActionScript;
import com.model.KeyValue;

/**
 * Usage:
 * 
 * 
 * 
 */

public class ActionBarController
        extends ServletDependenceController
        implements IActionBarDatasource, IServiceLayerDelegate {

	private ActionScriptParser actionScriptParser = null;



	public ActionScriptParser getActionScriptParser () {

		return actionScriptParser;
	}



	public void setActionScriptParser ( ActionScriptParser actionScriptParser ) {

		this.actionScriptParser = actionScriptParser;
	}

	private ActionBarServiceLayer actionBarserviceLayer = null;
	ArrayList <ActionBarItemView> actionBarItemsList    = null;



	private String userContextName () {

		SessionManager sessionManager = SessionManager.sharedInstance();
		return (String) sessionManager.getUserContextName();
	}



	@Override
	public <T> void responseResponseItemsList ( final List <T> items,
	                                            final Object request,
	                                            final Exception exception ) {

		actionBarItemsList = new ArrayList <ActionBarItemView>();
		if ( exception != null ) {
			return;
		}
		if ( items == null ) {
			return;
		}

		for ( T item : items ) {

			String linkURL = null;
			String onClickEvent = null;

			ActionBarItem actionBarItem = (ActionBarItem) item;
			if ( this.actionScriptParser == null ) {
				continue;
			}

			ActionScript actionScript = actionBarItem.getActionScript();
			if ( actionScript == null ) {
				continue;
			}

			String script = actionScript.getActionScriptText();
			String scriptType = actionScript.getActionScriptType();

			boolean errorOccured = false;

			Set <KeyValue> actionScriptParams = actionScript.getParams();
			for ( KeyValue param : actionScriptParams ) {

				String paramValue = null;
				try {
					paramValue = actionScriptParser.parseActionScriptParam(param);
				} catch ( Exception e ) {
					e.printStackTrace();
				}

				if ( paramValue == null ) {
					errorOccured = true;
				}

				if ( !errorOccured ) {
					script = script.replaceAll("\\{" + param.getKey() + "\\}", paramValue);
				}
			}

			if ( errorOccured ) {
				continue;
			}

			if ( "javascript".equalsIgnoreCase(scriptType) ) {
				onClickEvent = script;
			} else {
				linkURL = script;
			}


			ActionBarItemView actionBarItemView = new ActionBarItemView();
			actionBarItemView.setStyleClassName("action_bar_navigator_item");
			actionBarItemView.setLinkStyleClassName("action_bar_navigator_item_link");
			actionBarItemView.setName(actionBarItem.getName());
			actionBarItemView.setLinkURL(linkURL);
			actionBarItemView.setOnClickEvent(onClickEvent);

			actionBarItemsList.add(actionBarItemView);

		}
	}



	@Override
	public void finalize ()
	        throws Throwable {

		try {
			if ( actionBarserviceLayer != null ) {
				actionBarserviceLayer.setLayerDelegate(null);
				actionBarserviceLayer = null;
			}
		} finally {
			super.finalize();
		}
	}



	/**
	 * 
	 * ICustomDatasource
	 */

	@Override
	public void reloadData () {


		String userContextName = this.userContextName();
		int userId = LoginManager.sharedInstance().getUserId();

		actionBarserviceLayer = new ActionBarServiceLayer();
		actionBarserviceLayer.setLayerDelegate(this);
		actionBarserviceLayer.performRequestWithUserContextAndUserId(userContextName, userId);

	}



	@Override
	public void setCustomDatasourceDelegate ( ICustomDatasourceDelegate delegate ) {

		// TODO Auto-generated method stub

	}



	/**
     *
     *
     *
     */

	@Override
	public int itemsCount () {

		if ( actionBarItemsList == null ) {
			return 0;
		} else {
			return actionBarItemsList.size();
		}
	}



	@Override
	public ActionBarItemView itemAtIndex ( int itemIndex ) {

		if ( actionBarItemsList == null ) {
			return null;
		} else {
			return actionBarItemsList.get(itemIndex);
		}

	}

}
